package com.tianxinweb.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.tianxinweb.entity.Result;
import com.tianxinweb.entity.Role;
import com.tianxinweb.enums.ResultEnum;
import com.tianxinweb.exception.MyException;
import com.tianxinweb.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cboy
 * @since 2018-07-19
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 获取所有角色
     * @return
     */
    @RequestMapping("getAllRole")
    public Result getAllRole() {
        List<Role> list = roleService.selectList(null);
        return Result.success().add("roles", list);
    }

    /**
     * 获取/搜索角色列表
     * 分页每页默认显示10条
     * @param pn
     * @param size
     * @return
     */
    @RequestMapping("list")
    public Result list(@RequestParam(value="pn",defaultValue="1") Integer pn,
                       @RequestParam(value="size",defaultValue="10") Integer size,
                       @RequestParam(value = "key",defaultValue = "") String key) {
       Page page = roleService.getRoleList(pn,size,key);
        return Result.success().add("page", page);
    }

    /**
     * 添加角色
     * @param role
     * @param reuslt
     * @return
     */
    @PostMapping("add")
    public Result add(@RequestBody @Valid Role role, BindingResult reuslt){
        if(reuslt.hasErrors()){
            throw new MyException(ResultEnum.PARAMETER_FORMAT_ERROR);
        }
        Role role1 = roleService.selectByRoleName(role.getRoleName());
        if(role1 != null) throw new MyException(ResultEnum.ROLE_NAME_USED);
        roleService.addRole(role);
        return Result.success();
    }

    /**
     * 检查角色名称是否可用
     * @param roleName
     * @return
     */
    @RequestMapping("checkRoleNameUnique")
    public Result checkRoleNameUnique(@RequestParam("roleName") String roleName){
        if(roleName.trim().length()>32){
            throw new MyException(ResultEnum.PARAMETER_FORMAT_ERROR);
        }
        Role role = roleService.selectByRoleName(roleName);
        return role==null?Result.success():Result.fail();
    }

    /**
     * 修改角色
     */
    @PostMapping("update")
    public Result update(@RequestBody @Valid Role role,BindingResult result){
        if(result.hasErrors() || role.getRoleId()==null){
            throw new MyException(ResultEnum.PARAMETER_FORMAT_ERROR);
        }
        Role role1 = roleService.selectByRoleName(role.getRoleName());
        if(role1 != null){
            if(role.getRoleId() != role1.getRoleId()){
                throw new MyException(ResultEnum.ROLE_NAME_USED);
            }
        }
        Role role2 = roleService.selectById(role.getRoleId());
        if(role2 ==null) return Result.fail();
        roleService.updateRole(role);
        return Result.success();
    }

    /**
     * 通过id删除角色
     * @param id
     * @return
     */
    @RequestMapping("delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        roleService.deleteById(id);
        return Result.success();
    }

    /**
     * 通过id获取角色信息
     * @param id
     * @return
     */
    @RequestMapping("getRoleById/{id}")
    public Result getRoleById(@PathVariable("id") Integer id){
        Role role = roleService.selectById(id);
        return Result.success().add("role", role);
    }

}

